<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class News extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $news = db('news')->paginate(10);
        $this->assign('news', $news);
        return view();
    }

    // 添加/编辑新闻
    public function save_news() {
        $id = input('param.id');
        $cates = get_cates('news_category', 0);
        $this->assign('cates', $cates);
        if (Request::instance()->isPost()) {
            // 提交数据
            $data = [
                'name' => input('param.name'),
                'cate_id' => input('param.cate_id'),
                'add_time' => time()
            ];

            $post_image = input('param.image', '');
            if ($post_image) {
                // 如果图片有值  则获取旧图并删除
                $image = db('news')->where('id', $id)->value('image');
                if ($image) {
                    unlink('./uploads/nav/'.$image);
                }
                $data['image'] = $post_image;
            }

            if ($id > 0)  {
                $text = '编辑';
                $rs = db('news')->where('id', $id)->update($data);
                $url = url('index');
                db('news_content')->where('news_id', $id)->update(['content'=> input('param.content', ''), 'info' => input('param.info', '')]);
            } else {
                $text = '添加';
                $rs = db('news')->insertGetId($data);
                $url = url('save_news', 'id=' . $rs);
                db('news_content')->insert(['content'=> input('param.content', ''), 'news_id' => $rs]);
            }
            if ($rs)
                return json(['code' => 200, 'msg' => $text . '新闻成功！', 'url' => $url]);
            else
                return json(['code' => 0, 'msg' => $text . '新闻失败！']);
        } else {
            if ($id > 0) {
                $news = db('news')->alias('n')->field('n.*, nc.content, nc.info')
                    ->join('osl_news_content nc', 'n.id = nc.news_id', 'LEFT')->where('n.id', $id)->find();
                $this->assign('news', $news);
            }
            
            return view();
        }
    }

    // 文章分类
    public function news_category() {
        $cate = get_cates('news_category', 0);
        $this->assign('cate', $cate);
        return view();
    }

    // 添加/编辑新闻分类
    public function save_category() {
        $id = input('param.id', 0);
        $cates = get_cates('news_category', 0);
        $this->assign('cates', $cates);
        if (Request::instance()->isPost()) {
            // 提交数据
            $data = [
                'pid' => input('param.pid'),
                'name' => input('param.name'),
                'level' => 1
            ];
            if (input('param.pid') > 0) {
                $cate = db('news_category')->where('id', input('param.pid'))->find();
                if ($cate['level'] >= 2) {
                    return json(['code' => 0, 'msg' => '分类最多二级，请确认！']);
                } else{
                    $data['level'] = $cate['level'] + 1;
                }
            }

            if ($id > 0)  {
                $text = '编辑';
                $rs = db('news_category')->where('id', $id)->update($data);
            } else {
                $text = '添加';
                $rs = db('news_category')->insert($data);
            }
            if ($rs !== false)
                return json(['code' => 200, 'msg' => $text . '文章分类成功！']);
            else
                return json(['code' => 0, 'msg' => $text . '文章分类失败！']);
        } else {
            $text = $id > 0 ? '编辑' : '添加';
            $cate = [];
            if ($id > 0) {
                $cate = db('news_category')->where('id', $id)->find();
            }
            $this->assign('cate', $cate);
            $this->assign('text', $text);
            return view();
        }
    }

    // 删除分类
    public function del_cate() {
        $id = input('param.id', 0);
        $news = db('news')->where('cate_id', $id)->count();
        if ($news > 0) {
            return json(['code' => 0, 'msg' => '分类下面有新闻，不能删除！']);
        }

        $category = db('news_category')->where('pid', $id)->count();
        if ($category > 0) {
            return json(['code' => 0, 'msg' => '分类下面有子类，不能删除！']);
        }
        $rs = db('news_category')->where('id', $id)->delete();
        if ($rs !== false) {
            return json(['code' => 200, 'msg' => '删除成功！']);
        } else {
            return json(['code' => 0, 'msg' => '删除失败！']);
        }
    }
}
